package com.apes.fn.business.fixedLoss.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.business.fixedLoss.modle.Maintenance;
import com.apes.fn.business.fixedLoss.repository.MaintenanceRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName : MaintenanceService
 * @Description : 三方维修单业务层
 * @Author : ZhengZeXian
 * @Date: 2020年11月18日15:36:46
 */
@Service("maintenanceService")
public class MaintenanceService extends DomainService {

    @Autowired
    private MaintenanceRepository maintenanceRepository;

    /**
     * 保存三方维修单
     * @param request
     * @return
     */
    public Maintenance save(SimpleRequest request){
        Maintenance maintenance = request.getO(Maintenance.class);
        //检查单据类型
        checkBusinessTyp(maintenance);
        maintenance.computeAmt();//总计
        maintenance.setState("create");
        maintenance.setCompany(maintenance.getDept().getCompany());
        return maintenanceRepository.save(maintenance);
    }

    /**
     * 更新三方维修单
     * @param request
     * @return
     */
    public Maintenance update(SimpleRequest request){
        checkData(request);
        Maintenance maintenance = request.getO(Maintenance.class);
        //检查单据类型
        checkBusinessTyp(maintenance);
        maintenance.computeAmt();//总计
        return maintenanceRepository.saveAndFlush(maintenance);
    }

    /**
     * 终止
     * @param request
     * @return
     */
    public String repeal(SimpleRequest request){
        checkData(request);
        Maintenance maintenance = request.getO(Maintenance.class);
        maintenance.setState("repeal");
        maintenanceRepository.saveAndFlush(maintenance);
        return "终止成功";
    }

    /**
     * 检查数据
     * @param request
     * @return
     */
    private void checkData(SimpleRequest request) {
        String id = request.get("id");
        // 获取当前请求的版本信息
        int versions = request.get("version");
        Maintenance maintenance = maintenanceRepository.findOne(id);
        if (maintenance == null) throw new RuntimeException("三方维修单【" + id + "】在系统中不存在。");
        if (maintenance.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    /**
     * 检查单据类型
     * @param maintenance
     */
    private void checkBusinessTyp(Maintenance maintenance){
        if(maintenance.getBusinessType().getName().equals("定损销售")){
            if (maintenance.getLossPlace() == null) throw new RuntimeException("定损销售请填写三方定损点");
            if (!(maintenance.getIntroduceMoney()>=0)) throw new RuntimeException("请填写正确的佣金金额");
        }
    }

    /**
     * 外协工单查询条件封装
     * @param request
     * @return
     */
    private Map fnOutWorkOrderQuery(SimpleRequest request){
        return orderIdField(request);
    }

    /**
     * 外协派工查询条件封装
     * @param request
     * @return
     */
    private Map fnDispatchOrderQuery(SimpleRequest request){
        return orderIdField(request);
    }

    private Map orderIdField(SimpleRequest request){
        JSONObject jo = request.getJO();
        JSONArray oldConditions = jo.getJSONArray("condition");
        JSONArray newConditions = new JSONArray();
        for (Object o : oldConditions) {
            JSONObject condition = (JSONObject) o;
            String field = condition.getString("field");
            String value = condition.getString("value");
            if (field.equals("ORDER_ID")){
                if (changeConditions(value,"FW","SERVICE_PLAN_ID",newConditions)) continue;
                if (changeConditions(value,"DS","LOSS_QUOTATION_ID",newConditions)) continue;
            }
            newConditions.add(condition);
        }
        jo.put("condition",newConditions);
        return new com.apestech.framework.json.JSONObject(jo);
    }

    private boolean changeConditions(String value,String containStr,String newField,JSONArray newConditions){
        if (!value.contains(containStr)) return false;
        JSONObject tempJO = new JSONObject();
        tempJO.put("field",newField);
        tempJO.put("operator","EQ");
        tempJO.put("value",value);
        newConditions.add(tempJO);
        return true;
    }
}
